package com.murphy.algorithm.likou;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @author dongsufeng
 * @version 4.0
 * @date 2023/2/21 10:32
 */
public class StackDemo {

    public boolean isValid(String str){
        Stack<Character> stack = new Stack<>();
        Map<Character,Character> map = new HashMap<>();
        map.put(')','(');
        map.put(']','[');
        for (int i = 0; i <str.length(); i++) {
            char curr=str.charAt(i);
            if (stack.isEmpty()){
                if ( map.containsKey(curr)) {
                    return false;
                }else {
                    stack.push(curr);
                }
            }else {
                if (map.containsKey(curr)){
                    char peek=stack.peek();
                    if (peek==map.get(curr)){
                        stack.pop();
                    }else {
                        return false;
                    }
                }else {
                    stack.push(curr);
                }
            }

        }
        if (stack.isEmpty()){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String s ="[]()[]([])";
        StackDemo stackDemo = new StackDemo();
        System.out.println(stackDemo.isValid(s));
    }
}
